package com.liqi.frame.comm.exception;

import cn.hutool.core.lang.Console;
import com.liqi.frame.comm.dto.HttpResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.InvalidClassException;

/**
 * 全局异常处理类
 * @author liqi on 2020/4/24 0024. liqiwork@qq.com
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(value = {Exception.class,RuntimeException.class})
    @ResponseBody
    public HttpResult error500(HttpServletRequest request, Exception e){
        Console.error(e);
        Console.error(request.getRequestURL()+" 出错了，跳到500页面。");
        return HttpResult.failure("未携带令牌");
    }

    @ExceptionHandler(value = {GlobalException.class})
    @ResponseBody
    public HttpResult errorGlobal(HttpServletRequest request, GlobalException e){
        Console.error(e);
        Console.error(request.getRequestURL()+" 出错了，跳到500页面。");
        return HttpResult.failure("500错误");
    }


    @ExceptionHandler(value = {InvalidClassException.class})
    @ResponseBody
    public HttpResult errorInvalid(HttpServletRequest request, GlobalException e){
        Console.error(e);
        Console.error(request.getRequestURL()+" 出错了，跳到500页面。");
        return HttpResult.failure("500错误");
    }

}
